﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

using Microsoft.Xna.Framework.Storage;


namespace Aphysoft.Vate
{
    /// <summary>
    /// Helper class which stores all used directories.
    /// </summary>
    public static class PathHelper
    {
        #region Game base directory
        /// <summary>
        /// We can use this to relocate the whole game directory to another
        /// location. Used for testing (everything is stored on a network drive).
        /// </summary>
        public static readonly string GameBaseDirectory =
            // Update to support Xbox360:
            StorageContainer.TitleLocation;
        //"";
        #endregion

        /// <summary>
        /// Checks if the specified file name is exists or not.
        /// </summary>
        /// <param name="fileName">Complete file path.</param>
        public static bool FileExists(string fileName)
        {
            FileInfo fin = new FileInfo(fileName);
            return fin.Exists;
        }
        /// <summary>
        /// Gets directory path of specified file path.
        /// </summary>
        /// <param name="fileName">Complete file path.</param>
        public static string GetDirectory(string fileName)
        {
            FileInfo fin = new FileInfo(fileName);
            return StringHelper.Create(fin.DirectoryName, '\\');
        }
        /// <summary>
        /// Gets relative directory path of specified file path.
        /// </summary>
        /// <param name="filename">File path.</param>
        /// <param name="topmost">Out topmost file path.</param>
        /// <param name="right">Out path without topmost path.</param>
        public static string GetRelativeDirectory(string filePath, out string topmost, out string right)
        {
            string path = filePath.Replace('/', '\\').Trim('\\');

            string[] paths = path.Split('\\');

            if (paths.Length > 0)
            {
                topmost = paths[0];

                // create left
                if (paths.Length > 1)
                {
                    StringBuilder sb = new StringBuilder();
                    string returned = string.Empty;
                    for (int i = 1; i < paths.Length; i++)
                    {
                        if (i == paths.Length - 1)
                        {
                            returned = StringHelper.Create(topmost, '\\', sb.ToString());
                        }

                        sb.Append(paths[i]);
                        if (i < paths.Length - 1)
                        {                            
                            sb.Append('\\');
                        }
                    }
                    right = sb.ToString();
                    return returned;
                }
                else
                {                    
                    right = string.Empty;
                    return string.Empty;
                }  
                //      path\to\direction\file.txt
                // topmost: path
                // return: path\to\direction\
                // right: to\direction\file.txt
            }
            else
            {
                topmost = string.Empty;
                right = string.Empty;
                return string.Empty;
            }
        }
        /// <summary>
        /// Gets relative directory path of specified file path.
        /// </summary>
        /// <param name="fileName">File path.</param>
        public static string GetRelativeDirectory(string fileName)
        {
            string tm, r;
            return GetRelativeDirectory(fileName, out tm, out r);
        }
        /// <summary>
        /// Gets extension of specified file name / file path.
        /// </summary>
        /// <param name="fileName">File name or file path.</param>
        public static string GetFileExtension(string fileName)
        {
            string[] ss = fileName.Split('.');

            if (ss.Length > 0)
            {
                return ss[ss.Length - 1];
            }
            else
                return string.Empty;
        }
    }
}
